
%% Symbolic Toolbox Examples

%see 'doc sym' for many more


%Create a symbolic variable
x=sym('x')

%y is function of x
y=x^2

y^2

%Indefinite Integral
Y=int(y)

%Derivative
y_p=diff(y)

%% Plotting
figure
ezplot(y,[-10 10])
hold on
ezplot(y_p,[-10 10])
ezplot(Y,[-10 10])

syms t
figure
ezplot3(t^2*sin(10*t), t^2*cos(10*t), t)


syms x y
figure
ezsurf(x^2 + y^2)
hold on
zlabel('z')
title('z = x^2 + y^2')
hold off

%many other 'ez' versions of standard plots


%% Solving Equations
syms a b c y

%Solve an equation
solve( a*x^2+b*x+c==0,x)

%Solve system of equations
Out=solve(x-y==5, x^2+y==4,{x,y})

Out.x
Out.y

simplify(Out.x)


%% Solving ODEs

% Wave equation
syms y(t)  L
D2y = diff(y,2);
Dy = diff(y);
Ysol=dsolve(D2y == -y)

%Exponential
syms y(t) k L
D2y = diff(y,2);
Dy = diff(y);
Ysol=dsolve(Dy == -k*y)


         

%with boundary condition
Ysol=dsolve(Dy == -k*y,y(0)==100)

%put in a specific 'k' and plot
Ysubk=subs(Ysol,k,3)
figure
ezplot(Ysubk,[0 5 0 100])


%% Create function handle

syms x y
r = sin(x);
ht = matlabFunction(sin(r)/r)

Xvec=linspace(0,10,100)
Yvec=ht(Xvec)
figure
plot(Xvec,Yvec)
xlabel('X')
ylabel('Y')

%% Show latex

latex(Ysol)
figure
h = text('string',['$' latex(Ysol) '$'],...
             'interpreter','latex',...
             'fontsize',30,...
             'units','norm',...
             'pos',[.1 .5]);

         
%% Taylor Series

syms x
f = 1/(5 + 4*cos(x));
T = taylor(f, 'Order', 8)

pretty(T)

figure
h = text('string',['$' latex(T) '$'],...
             'interpreter','latex',... 
             'fontsize',30,...
             'units','norm',...
             'pos',[.1 .5]);












